package com.example.chen.tabindicatorlib;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.chen.tabindicatorlib.fragment.VpSimpleFragment;
import com.example.chen.tabindicatorlib.widget.ViewPageIndicator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPageIndicator mIndicator;
    private ViewPager mPager;

    private String[] mTitles=new String[]{"短信","电话","收藏","消息","邮件","安卓","苹果","电脑","手机"};

    private List<VpSimpleFragment> mFragments=new ArrayList<>();

    private MyAdapter mAdater;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initData();

    }

    private void initView() {
        mIndicator = (ViewPageIndicator) findViewById(R.id.id_indicator);
        mIndicator.setmTitles(mTitles);
        mIndicator.setCurrentShapr(mIndicator.SHAPE_LINE);
        mPager = (ViewPager) findViewById(R.id.id_pager);

    }

    private void initData() {
        for(int i=0;i<mTitles.length;i++){
            mFragments.add(VpSimpleFragment.newInstance(mTitles[i]));
        }
        mAdater = new MyAdapter(getSupportFragmentManager());
        mPager.setAdapter(mAdater);
        mIndicator.setmViewPager(mPager,0);
    }


    public class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
